<?php
/**
 * Desc:
 * User: yangyang
 * Date: 2024/4/8 15:33
 * Ver: 1.0
 */

namespace App\Transformers;

use App\Models\Order;
use League\Fractal\TransformerAbstract;

class OrderTransformer extends TransformerAbstract
{
    public $availableIncludes = ['orderDetails','user'];

    public function transform(Order $order)
    {
        return [
            'id'           => $order->id,
            'order_no'     => $order->order_no,
            'amount'       => $order->amount,
            'status'       => $order->status,
            'address_id'      => $order->address_id,
            'express_type' => $order->express_type,
            'express_no'   => $order->express_no,
            'pay_time'     => $order->pay_time,
            'pay_type'     => $order->pay_type,
            'trade_no'     => $order->trade_no,
            'created_at'   => $order->created_at->toDateTimeString(),
            'updated_at'   => $order->updated_at->toDateTimeString(),
        ];
    }

    public function includeOrderDetails(Order $order)
    {
        return $this->collection($order->orderDetails, new OrderDetailTransformer());
    }

    public function includeUser(Order $order)
    {
        return $this->item($order->user, new UserTransformer());
    }
}
